<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Lang;
use Douyin\Open\Api\DataExternalItemApi;
/**
 * Ajax异步请求接口
 * @internal
 */
class Ajax extends Frontend
{

    protected $noNeedLogin = ['lang', 'upload',"updateVideo"];
    protected $noNeedRight = ['*'];
    protected $layout = '';

    /**
     * 加载语言包
     */
    public function lang()
    {
        header('Content-Type: application/javascript');
        header("Cache-Control: public");
        header("Pragma: cache");

        $offset = 30 * 60 * 60 * 24; // 缓存一个月
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");

        $controllername = input("controllername");
        $this->loadlang($controllername);
        //强制输出JSON Object
        $result = jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
        return $result;
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        return action('api/common/upload');
    }

    public  function  updateVideo()
    {
        if(config('site.severmit')==1) {
            $M_seller=new \app\admin\model\SellerLog();
            $datas = $M_seller->with('user')->where($M_seller->getTable().".createtime", ">", time() - (86400 * 7))->select();

            $apiInstance = new DataExternalItemApi();

            foreach ($datas as $data) {
                if ($data->user->id) {
                    $re = $apiInstance->dataExternalItemBaseGet($data->user->open_id, $data->user->open_token, $data->video_code);
                    $result = $re->getData()->getResult();
                    if ($result) {
                        $data->avg_play_duration = $result->getAvgPlayDuration();
                        $data->total_comment = $result->getTotalComment();
                        $data->total_like = $result->getTotalLike();
                        $data->total_play = $result->getTotalPlay();
                        $data->total_share = $result->getTotalShare();
                        $data->save();
                    }
                }

            }
        }

    }










}
